//
//  EnterViewController.m
//  维度
//
//  Created by 宋梦华 on 2019/1/23.
//  Copyright © 2019年 宋梦华. All rights reserved.
//

#import "EnterViewController.h"
#import "RegisterViewController.h"
#import "AESCrypt.h"
#import "MineViewController.h"
#import "RegisterModel.h"
#import "AppDelegate.h"
#import "MainViewController.h"
#import "YYModel.h"
#import "LoginModel.h"
#import "DataBase.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface EnterViewController ()

@end

@implementation EnterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//   背景图
    self.navigationController.navigationBar.hidden = YES;
    UIView *view =[[UIView alloc]initWithFrame:self.view.frame];
    UIImageView *img =[[UIImageView alloc]initWithFrame:self.view.frame];
    img.image =[UIImage imageNamed:@"Back"];
    [view addSubview:img];
    [self.view addSubview:view];
    
    [self.view addSubview:self.PhoneTF];
    [self.view addSubview:self.PasswordTF];
    [self.view addSubview:self.RememberBtn];
    [self.view addSubview:self.AutoEnterBtn];
    [self.view addSubview:self.QuickResignBtn];
    [self.view addSubview:self.EnterBtn];
    [self.view addSubview:self.Wechat];
    
    
}
-(UITextField *)PhoneTF{
    if (!_PhoneTF) {
        _PhoneTF = [[UITextField alloc]initWithFrame:CGRectMake((WIDTH-350)/2, 150, 350, 70)];
//        设置提示文字的颜色
        NSString *holderText = @"手机号";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont boldSystemFontOfSize:18]
                            range:NSMakeRange(0, holderText.length)];
        _PhoneTF.attributedPlaceholder = placeholder;
        _PhoneTF.textAlignment =NSTextAlignmentCenter;
//        边框线
        UIView * underLine = [[UIView alloc]initWithFrame:CGRectMake(0,60,350,1)];
        underLine.backgroundColor = [UIColor whiteColor];
        underLine.alpha = 0.5;
        [_PhoneTF addSubview:underLine];
        
//        手机图标
        UIImageView *img =[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 30)];
        img.image = [UIImage imageNamed:@"log_icon_phone_default_hdpi"];
        [_PhoneTF addSubview:img];
    }
    return _PhoneTF;
}
//密码文本框
-(UITextField *)PasswordTF{
    if (!_PasswordTF) {
        _PasswordTF = [[UITextField alloc]initWithFrame:CGRectMake((WIDTH-350)/2, 270, 350, 70)];
        //        设置提示文字的颜色
        NSString *holderText = @"登录密码";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor whiteColor]
                            range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont boldSystemFontOfSize:18]
                            range:NSMakeRange(0, holderText.length)];
        _PasswordTF.attributedPlaceholder = placeholder;
        _PasswordTF.textAlignment =NSTextAlignmentCenter;
//        密文
        _PasswordTF.secureTextEntry = YES;
        //        边框线
        UIView * underLine = [[UIView alloc]initWithFrame:CGRectMake(0,60,350,1)];
        underLine.backgroundColor = [UIColor whiteColor];
        underLine.alpha = 0.5;
        [_PasswordTF addSubview:underLine];
        //        密码图标
        UIImageView *img =[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 25, 30)];
        img.image = [UIImage imageNamed:@"log_icon_lock_default_hdpi"];
        [_PasswordTF addSubview:img];
//        小眼睛
        UIButton *eyeBtn =[[UIButton alloc]initWithFrame:CGRectMake(300, 10, 25, 40)];
        [eyeBtn setImage:[UIImage imageNamed:@"log_icon_eye_default_hdpi"] forState:UIControlStateNormal];
        [_PasswordTF addSubview:eyeBtn];
        
    }
    return _PasswordTF;
}
//记住密码按钮
-(UIButton *)RememberBtn{
    if (!_RememberBtn) {
        _RememberBtn =[[UIButton alloc]initWithFrame:CGRectMake((WIDTH-350)/2, 350, 80, 70)];
        [_RememberBtn setTitle:@"记住密码" forState:UIControlStateNormal];
        [_RememberBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
     }
    return _RememberBtn;
}
//自动登录按钮
-(UIButton *)AutoEnterBtn{
    if (!_AutoEnterBtn) {
        _AutoEnterBtn =[[UIButton alloc]initWithFrame:CGRectMake((WIDTH-350)/2+120, 350, 80, 70)];
        [_AutoEnterBtn setTitle:@"自动登录" forState:UIControlStateNormal];
        [_AutoEnterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    return _AutoEnterBtn;
}
//快速注册按钮
-(UIButton *)QuickResignBtn{
    if (!_QuickResignBtn) {
        _QuickResignBtn =[[UIButton alloc]initWithFrame:CGRectMake((WIDTH-350)/2+280, 350, 80, 70)];
        [_QuickResignBtn setTitle:@"快速注册" forState:UIControlStateNormal];
        [_QuickResignBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_QuickResignBtn addTarget:self action:@selector(QuickResign) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _QuickResignBtn;
}
//快速注册跳转至 注册页面
-(void)QuickResign{
    RegisterViewController *reg = [RegisterViewController new];
    [self.navigationController pushViewController:reg animated:YES];
}

//登录
-(UIButton *)EnterBtn{
    if (!_EnterBtn) {
        _EnterBtn =[[UIButton alloc]initWithFrame:CGRectMake((WIDTH-350)/2, 500, 350, 50)];
        [_EnterBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_EnterBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_EnterBtn setBackgroundColor:[[UIColor alloc]initWithRed:255/255.0 green:209/255.0 blue:0/255.0 alpha:1.0]];
        [_EnterBtn addTarget:self action:@selector(Enter) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _EnterBtn;
}
//登录 跳转至 主界面
-(void)Enter{
    [self.navigationController popViewControllerAnimated:YES];
    //加密
    NSString *message = @"123";
    //解密
    NSString *message1 = [AESCrypt decrypt:message password:self.PasswordTF.text];
    
    NSDictionary *dic = @{@"phone":self.PhoneTF.text,
                          @"pwd":message1
                          };
    AFHTTPSessionManager *manger=[AFHTTPSessionManager manager];
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manger POST:@"http://172.17.8.100/movieApi/user/v1/login" parameters:dic progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"//////%@,登录成功",responseObject);
        NSLog(@"//////%@,",responseObject[@"message"]);
//        NSString *token = [[responseObject objectForKey:@"userInfo"] objectForKey:@"user_Info"];
//        NSUserDefaults *ContentDefault = [NSUserDefaults standardUserDefaults];
//        [ContentDefault setObject:token forKey:@"token"];
//        [ContentDefault synchronize];
        
//        //取
//        NSString *tokens = [ContentDefault objectForKey:@"token"];
//        NSMutableDictionary *parm = [[NSMutableDictionary alloc] init];
//        [parm setObject:tokens forKey:@"token"];
//
//        //存
//        NSDictionary *SumDict = [NSDictionary new];
//        AppDelegate *app = (AppDelegate*)[UIApplication sharedApplication].delegate;
//        app.window.rootViewController = [[MainViewController alloc]init];
//        SumDict = responseObject[@"userInfo"];
//        LoginModel *loginM= [LoginModel yy_modelWithJSON:SumDict];
//
//        NSUserDefaults *UserInfo = [NSUserDefaults standardUserDefaults];
//        [UserInfo setObject:loginM.user_name forKey:@"user_name"];
//        [UserInfo setObject:loginM.user_sax forKey:@"user_sax"];
//        [UserInfo setObject:loginM.user_data forKey:@"user_data"];
//        [UserInfo setObject:loginM.user_email forKey:@"user_email"];
//        [UserInfo setObject:loginM.user_phone forKey:@"user_phone"];
//        [UserInfo setObject:loginM.user_password forKey:@"user_password"];
//
//        [UserInfo synchronize];
        dispatch_async(dispatch_get_main_queue(), ^{
            MineViewController *mine = [[MineViewController alloc]init];
            [self.navigationController pushViewController:mine animated:YES];

        });
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"密码错误,登录失败,%@",error);
    }];
// 登录注册简单全是bug实现:WD-Movies/LoginView/LoginVC/EnterViewController.m
}
//第三方微信
-(UIButton *)Wechat{
    if (!_Wechat) {
        _Wechat =[[UIButton alloc]initWithFrame:CGRectMake((WIDTH-50)/2, 650, 50, 50)];
        [_Wechat setBackgroundImage:[UIImage imageNamed:@"pay_icon_weixin_default_hdpi"] forState:UIControlStateNormal];
        [_Wechat addTarget:self action:@selector(wechat) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _Wechat;
}
//微信第三方登录页面
-(void)wechat{
    
}




@end
